import QtQuick
import QtQuick.Controls

Item {
    id: root

    // width: childrenRect.width
    // height: childrenRect.height


    property var sizeRange: Qt.vector2d(40, 60)       // 大小随机
    property var itemList: [rec]                      // 组件
    property var opacityRange: Qt.vector2d(0.5, 0.8)  // 透明度随机

    // 旋转动画
    property real rotationTime: 5
    property bool rotationOpen: true

    // 闪烁动画
    property bool bling: true
    property var blingTimeRange: Qt.vector2d(1200, root.rotationTime * 1000) // 闪烁时间随机

    // Item {
    //     id: root

    //     anchors.centerIn: parent
        property real scale: 1.0

        property real size: getRandom(sizeRange)
        width: size * scale
        height: size * scale

        opacity: getRandom(opacityRange)

        Loader {
            anchors.fill: parent
            id: particleLoader
        }
    // }

    PropertyAnimation {
        id: rotationAni
        target: root
        property: 'rotation'

        from: 0 + 360 * Math.random()
        to: Math.random() > 0.5? 360 + from : -360 + from
        duration: root.rotationTime * 1000

        loops: Animation.Infinite
    }

    ParallelAnimation{
        id: blingAni
        property real duration: getRandom(root.blingTimeRange)

        SequentialAnimation {

            PauseAnimation {
                duration: 0.4 * blingAni.duration
            }

            PropertyAnimation {
                target: root
                property: 'opacity'

                to: 0

                duration: 0.6 * blingAni.duration
            }
        }

        SequentialAnimation {
            PropertyAnimation {
                target: root
                property: 'scale'

                from: root.scale
                to: 1.6 * root.scale

                duration: 0.25 * blingAni.duration
            }

            PropertyAnimation {
                target: root
                property: 'scale'

                from: 1.6 * root.scale
                to: 0.5 * root.scale

                duration: 0.25 * blingAni.duration

                onStopped: {
                    root.scale = 0.3 * root.scale
                }
            }

            PropertyAnimation {
                target: root
                property: 'scale'

                from: 0.5 * root.scale
                to: 1.0 * root.scale

                duration: 0.25 * blingAni.duration
            }

            PropertyAnimation {
                target: root
                property: 'scale'

                from: 0.8 * root.scale
                to: 0

                duration: 0.25 * blingAni.duration

                onStopped: {
                    root.scale = 0.3 * root.scale
                }
            }
        }
    }

    Component {
        id: rec
        Rectangle {
            anchors.fill: parent
            color: Qt.rgba(Math.random(), Math.random(), 0.0, 1)
        }
    }

    Component.onCompleted: {
        var itemNum = itemList.length
        particleLoader.sourceComponent = itemList[Math.floor(Math.random() * itemNum)]
        if (root.rotationOpen) {
            rotationAni.start()
        }
        if (bling) {
            blingAni.start()

        }
    }

    function getRandom(sizeRange){
        return sizeRange.x + Math.random() * (sizeRange.y - sizeRange.x)
    }
}
